package com.dating.view;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.dating.activity.Matching;
import com.dating.entities.Her;
import com.google.android.maps.MapView;

public class MapUserAdapter extends BaseAdapter{
	Context context;
	MapView mapView;
	ArrayList<Her> array;
	ArrayList<DtItemMap> listView=new ArrayList<DtItemMap>();
	DtOverlay mOvlerlay;
	
	public MapUserAdapter(Context context,MapView mapView,DtOverlay mOvlerlay,ArrayList<Her> array) {
		this.context=context;
		this.mapView=mapView;
		this.mOvlerlay=mOvlerlay;
		this.array=array;
		
		for(Her _her: array){
			DtItemMap _view=new DtItemMap(context, _her._status, _her._fullname, _her._bmp, _her);
			listView.add(_view);
		}
	}
	@Override
	public int getCount() {
		return array.size();
	}

	@Override
	public Object getItem(int position) {
		return array.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		DtItemMap _view=(DtItemMap)convertView;
		if(_view==null){
			Her her=array.get(position);
			_view=new DtItemMap(context, her._status, her._fullname, her._bmp, her);
		}
		//set on click
		_view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(listView.get(position)!=null)
					animatedTo(((DtItemMap) listView.get(position)).her.getLocation());
			}
		});
		
		_view.tv_name.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				matching(listView.get(position));
			}
		});
		_view.tv_distance.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				matching(listView.get(position));
			}
		});
		return _view;
	}
	public void animatedTo(Location l){
		mOvlerlay.animatedTo(l);
	}
	public void matching(View v){
		if(v!=null){
			String id=((DtItemMap)v).getHerId();
			Intent matchingIntent=new Intent(context, Matching.class);
			Bundle b=new Bundle();
			b.putString("id_her", id);
			matchingIntent.putExtras(b);
			context.startActivity(matchingIntent);
		}
	}
}
